<?php
require 'includes/config.php';
require 'includes/auth.php';
require 'includes/sanitize.php';

$data = sanitize($_GET);
$accesstoken = $_SESSION['accesstoken'];

// Check if something is posted in the form
if (isset($data['submit'])) {
	// resolv security level
	switch ($data['securitylevel']){
		case "Open for all":
			$level = 1;
			break;
		case "Friends only":
			$level = 2;
			break;
		case "Invite only":
			$level = 3;
			break;
	}
	// Try to update league
	$request = new makeRequest($backend.'api.php', 'POST', 'method=league&id='.$data['id'].
	'&newname='.$data['name'].'&description='.$data['description'].'&playerlimit='.$data['playerlimit'].
	'&securitylevel='.$level.'&accesstoken='.$accesstoken.'&auid='.$_SESSION['uid']);
	$tmp = $request->makeRequest();
	$http_code = $tmp[1];
	// Check for errors
	if($http_code == 200){
		$msg="League updated!";
	}else{
		$msg="Could not update league ".$http_code;
	}
}
// Retrieve info about the league the user wants to edit
if(isset($data['id'])){
	$request = new makeRequest($backend.'api.php?method=league&id='.$data['id'], 'GET');
	$tmp = $request->makeRequest();
	$rbody = $tmp[0];
	$http_code = $tmp[1];
	$obj = json_decode($rbody);
	// Set the info variables
	if($http_code == 200){
		$obj = json_decode($rbody);
		$id =				$obj[0]->{'id'};
		$name = 			$obj[0]->{'name'};
		$description = 		$obj[0]->{'description'};
		$playerlimit = 		$obj[0]->{'playerlimit'};
		$securitylevel =	$obj[0]->{'securitylevel'};
		// Resolve security level
		switch ($securitylevel){
			case 1:
				$level = "Open for all";
				break;
			case 2:
				$level = "Friends only";
				break;
			case 3:
				$level = "Invite only";
				break;
			default:
				$level = "Unkown";
		}
	}else{ // Something went wrong
		$msg="Could not find league ".$http_code;
	}
}
require 'includes/header.php';
require 'includes/topmenu.php';
echo '<div id="kaka">';
require 'includes/sidemenu.php'
?>
<div id="content"><?php
// Content shown on page
echo $msg;
?> <!--
This is the form shown on the page, with the info about the league pre filled in
 -->
<table>
	<form method="GET" action="<?php echo $PHP_SELF; ?>"><input
		type="hidden" name="id" value="<?php echo $id; ?>" />
	<tr>
		<td>League name:<br />
		</td>
		<td><input type="text" name="name" value="<?php echo $name; ?>" /><br />
		</td>
	</tr>
	<tr>
		<td>Description: <br />
		</td>
		<td><input type="text" name="description"
			value="<?php echo $description; ?>" /><br />
		</td>
	</tr>
	<tr>
		<td>Player limit: <br />
		</td>
		<td><select name="playerlimit">
		<?php if(isset($playerlimit)){

			echo '<option id="old">'.$playerlimit.'</option>';
		} ?>
			<option id="5">5</option>
			<option id="10">10</option>
			<option id="15">15</option>
			<option id="20">20</option>
			<option id="30">30</option>
			<option id="unlimited">999999</option>
		</select></td>
	</tr>
	<tr>
		<td>Security level: <br />
		</td>
		<td><select name="securitylevel">
		<?php if(isset($level)){

			echo '<option id="old">'.$level.'</option>';
		} ?>
			<option id="Open for all">Open for all</option>
			<option id="Friends only">Friends only</option>
			<option id="Invite onlys">Invite only</option>
		</select></td>
	</tr>
	<tr>
		<td><input type="submit" Value="Update" name="submit" />
	
	</form>
	</td>
	<td></td>
	</tr>
</table>
		<?php
		require 'includes/footer.php';
		?>